<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<nav class="navbar navbar-default .navbar-fixed-top">
    <div class="container-fluid">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse"
                    data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="/home/">七仔家园</a>
        </div>

        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav">
                <li class="active"><a href="/home/">博客 <span class="sr-only">(current)</span></a></li>
                <li><a href="#">学堂</a></li>
            </ul>
            <ul class="nav navbar-nav navbar-right">
                {% if request.user.is_authenticated %}
                    <li><a href="/mysite/{{ request.user.username }}/">{{ request.user.username }}</a></li>
                    <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true"
                           aria-expanded="false">更多操作 <span class="caret"></span></a>
                        <ul class="dropdown-menu">
                            <li><a href="#" data-toggle="modal" data-target=".bs-example-modal-lg">修改密码</a></li>
                            <li role="separator" class="divider"></li>
                            <li><a href="/user/logout/">退出登录</a></li>
                        </ul>
                    </li>
                    <div class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog"
                         aria-labelledby="myLargeModalLabel">
                        <div class="modal-dialog modal-lg" role="document">
                            <div class="modal-content">
                                <div class="container-fluid">
                                    <div class="row">
                                        <div class="col-md-8 col-md-offset-2">
                                            <h2 class="text-center">修改密码</h2>
                                            <div class="form-group">
                                                <label for="">用户名:</label>
                                                <input type="text" class="form-control"
                                                       value="{{ request.user.username }}" disabled>
                                            </div>
                                            <div class="form-group">
                                                <label for="old_password">原密码:</label>
                                                <input type="password" class="form-control" id="old_password"
                                                       username="old_password">
                                            </div>
                                            <div class="form-group">
                                                <label for="new_password">新密码:</label>
                                                <input type="password" class="form-control" id="new_password"
                                                       username="new_password">
                                            </div>
                                            <div class="form-group">
                                                <label for="confirm_password">原密码:</label>
                                                <input type="password" class="form-control" id="confirm_password"
                                                       username="confirm_password">
                                            </div>
                                            <br>
                                            <div class="modal-footer">
                                                <button type="button" class="btn btn-default" data-dismiss="modal">
                                                    取消
                                                </button>
                                                <button type="button" class="btn btn-primary" id="id_commit">保存</button>
                                                <span style="color: red" id="error"></span>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                {% else %}
                    <li><a href="/user/register/">注册</a></li>
                    <li><a href="/user/login/">登录</a></li>
                {% endif %}
            </ul>
        </div><!-- /.navbar-collapse -->
    </div><!-- /.container-fluid -->
</nav>
<div class="container-fluid">
    <div class="row">
        <div class="col-md-8 col-md-offset-2">
             <h2 class="text-center">发布文章</h2>
    {#    直接利用form表单提交数据#}
    <form action="" method="post">
        {% csrf_token %}
        <p>标题</p>
        <div>
            <input type="text" name="title" class="form-control" id="title">
        </div>
        <hr>
        <p>内容</p>
        <div>
            <textarea name="content" id="id_content" cols="50" rows="10"></textarea>
        </div>
        <hr>
        <p>分类:</p>
        <div>
            {% for category in category_list %}
                <input type="radio" value="{{ category.pk }}" name="category">{{ category.name }} &nbsp;&nbsp;
            {% endfor %}
        </div>
        <hr>
        <p>标签:</p>
        <div>
            {% for tag in tag_list %}
                <input type="checkbox" value="{{ tag.pk }}" name="tag">{{ tag.name }} &nbsp;&nbsp;
            {% endfor %}
        </div>
        <hr>
        <p>发布类型:</p>
        <div>
            <input type="radio" value="original" name="type" checked>原创 &nbsp;&nbsp;
            <input type="radio" value="reprint" name="type">转载
        </div>
        <hr>
        <br>
        <input type="submit" class="btn btn-danger" id="commit">
    </form>
        </div>
    </div>
</div>
<script>
   //修改密码js
    $('#id_commit').click(function (){
        $.ajax({
            url: '/user/set_password/',
            type: 'post',
            data: {"username": $('#username').val(),
                    "old_password": $('#old_password').val(),
                    "new_password": $('#new_password').val(),
                    "confirm_password": $('#confirm_password').val(),
                    "csrfmiddlewaretoken": '{{ csrf_token }}'
            },
            success: function (args){
                if(args.code === 1000){
                    window.location.reload()
                }else{
                    $('#error').text(args.msg)
                }
            }
        })
    })

</script>
    //富文本编辑器js
    {% load static %}
    <script charset="utf-8" src="{% static 'kindeditor/kindeditor-all-min.js' %}"></script>
    <script>
        KindEditor.ready(function (K) {
            window.editor = K.create('#id_content', {
                width: '980px',
                height: '600px',
                resizeType: 1,
                uploadJson: '/upload_image/', // 上传图片的后端路径
                extraFileUploadParams: {
                    'csrfmiddlewaretoken': '{{ csrf_token }}'
                },
            });
        });
    </script>
</body>
</html>